package com.samples.dbConnection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.samples.entity.City;

public class ConnectToWorldDB {

	public static final String DRIVER = "com.mysql.jdbc.Driver";
	public static final String CONNECTION_STRING = "jdbc:mysql://localhost:3306/test, root, Passw0rd";

	public static List<City> connectToCityDB() throws SQLException {
		Connection connection = DriverManager.getConnection(
				"jdbc:mysql://localhost:3306/test", "root", "Passw0rd");
		System.out.println("baglanti saglandi..." + "\n");
		PreparedStatement statement = connection
				.prepareStatement("select * from world.city");
		ResultSet result = statement.executeQuery();
		List<City> cities = new ArrayList<>();
		while (result.next()) {
			City city = new City();
			city.setId(result.getInt(1));
			city.setName(result.getString(2));
			cities.add(city);
		}
		return cities;

	}

}
